<template>
  <div class="app-container calendar-list-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-button class="filter-item" type="primary" @click="handleCreate" icon="el-icon-plus">添加</el-button>
      <el-button class="filter-item" type="primary" @click="handleUpdate" icon="el-icon-edit">修改</el-button>
      <el-button class="filter-item" type="primary" @click="handleDel" icon="el-icon-delete">删除</el-button>
      <el-button class="filter-item" type="primary" @click="handleSync" icon="el-icon-refresh">同步</el-button>
    </div>

    <el-table size="small" :data="list" v-loading="listLoading" element-loading-text="正在查询中。。。" border fit highlight-current-row>
      <el-table-column align="center" label="选择" width="65">
        <template slot-scope="scope">
          <el-radio v-model="radio"
                    :label="scope.$index"
                    @change.native="changeCheckedRadio(scope.row)">&nbsp;</el-radio>
        </template>
      </el-table-column>
      <el-table-column align="center" label="ID" prop="id"  v-if=false>
      </el-table-column>
      <el-table-column align="center" label="插件名" prop="name">
      </el-table-column>
      <el-table-column align="center" label="角色" prop="roleContent">
      </el-table-column>
      <el-table-column align="center" label="创建时间" prop="dateCreated">
      </el-table-column>
      <el-table-column align="center" label="更新时间" prop="dateUpdated">
      </el-table-column>
      <el-table-column align="center" label="是否开启">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.enabledContent"
            active-color="#13ce66"
            inactive-color="#C0C0C0"
            @change="changeSwitch(scope.row)">
          </el-switch>
        </template>
      </el-table-column>
    </el-table>


    <!-- 添加或修改对话框 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form :rules="rules" ref="dataForm" :model="dataForm" status-icon label-position="left" label-width="100px" style='width: 400px; margin-left:50px;'>
        <el-form-item label="插件名" prop="name">
          <el-input v-model="dataForm.name" v-bind:disabled="disabledInput"></el-input>
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-input v-model="dataForm.role"></el-input>
        </el-form-item>
        <el-form-item label="是否开启" prop="enabled">
          <el-input v-model="dataForm.enabled"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="createData">确定</el-button>
        <el-button v-else type="primary" @click="updateData">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>


<script>
  import { listPlugin, createPlugin, delPlugin, updatePlugin, switchPlugin, syncPlugin} from '@/api/system'

  export default {
    name: 'pluginList',
    data() {
      const validateRoleAndEnabled = (rule, value, callback) => {
        if (new RegExp("[01]").test(value)) {
          callback()
        } else {
          callback(new Error('只能输入0或1'))
        }
      }
      return {
        list: [],
        listLoading: true,
        radio: '',
        textMap: {
          update: '编辑',
          create: '创建'
        },
        rules: {
          name: [{ required: true, message: '插件名不能为空', trigger: 'blur' }],
          role: [{ required: true, message: '插件角色不能为空', trigger: 'blur' },
            { required: true, trigger: 'blur', validator: validateRoleAndEnabled }],
          enabled: [{ required: true, message: '是否开启不能为空', trigger: 'blur'},
            { required: true, trigger: 'blur', validator: validateRoleAndEnabled }],
        },
        dataForm: {
          id: '',
          name: '',
          role: '',
          enabled: '',
          dateCreated: '',
          dateUpdated: ''
        },
        dialogFormVisible: false,
        dialogStatus: '',
        disabledInput: false //插件名不能改，zk会生成新的
      }

    },
    created() {
      this.getList()
    },
    methods: {
      getList() {
        this.listLoading = true
        listPlugin().then(response => {
          this.list = response.data.data
          this.listLoading = false
        }).catch(() => {
          this.list = []
          this.listLoading = false
        })
      },
      changeSwitch(data) {
        switchPlugin(data).then(response => {
          this.$notify({
            title: '成功',
            message: '操作成功',
            type: 'success',
            duration: 2000
          });
          this.getList();
        })
      },
      resetForm() {
        this.dataForm = {
          id: '',
          name: '',
          role: '',
          enabled: '',
          dateCreated: '',
          dateUpdated: ''
        }
      },
      handleCreate() {
        this.resetForm()
        this.dialogStatus = 'create'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      createData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            createPlugin(this.dataForm).then(response => {
              this.dialogFormVisible = false
              this.$notify({
                title: '成功',
                message: '创建成功',
                type: 'success',
                duration: 2000
              });
              this.getList();
            })
          }
        })
      },
      handleDel() {
        if(!this.dataForm.id) {
          return;
        }
        this.$confirm('此操作将永久删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delPlugin(this.dataForm).then(response => {
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            });
            this.getList()
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      handleUpdate() {
        if(!this.dataForm.id) {
          return;
        }
        this.dialogStatus = 'update';
        this.dialogFormVisible = true;
        this.disabledInput = true;
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      updateData() {
       this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            updatePlugin(this.dataForm).then(() => {
              this.dialogFormVisible = false
              this.$notify({
                title: '成功',
                message: '更新成功',
                type: 'success',
                duration: 2000
              });
              this.disabledInput = false;
              this.getList()
            })
          }
        })
      },
      changeCheckedRadio(data) {
        this.dataForm = Object.assign({}, data)
      },
      handleSync() {
        syncPlugin().then(response => {
          this.dialogFormVisible = false
          this.$notify({
            title: '成功',
            message: '同步成功',
            type: 'success',
            duration: 2000
          });
        })
      }
    }
  }
</script>
